package com.vrv.common.resolver;

import java.util.HashMap;
import java.util.Map;

import com.vrv.common.resolver.dispatcher.DbType;
import com.vrv.common.utils.StringUtils;
import com.vrv.common.utils.UrlPrefix;

/**
 * @Description 请求参数
 * 
 * @author 曹保利
 */
public class RequestParameter {

	private DbType dbType = DbType.remotePageDb;

	/**
	 * 服务URl
	 */
	public String url;

	/**
	 * 每页显示记录数
	 */
	private int pageSize = 20;

	/**
	 * 请求的当前页号
	 */
	private int pageNumber = 0;

	/**
	 * 请求参数
	 */
	private Map<String, Object> args = new HashMap<String, Object>();

	/**
	 * 排序相关 (默认升序 ASC)
	 **/
	public Map<String, Boolean> sortMap = new HashMap<String, Boolean>();

	/**
	 * 比较相关 (默认大于 greater than short for ‘gt’)
	 **/
	public Map<String, Boolean> compareMap = new HashMap<String, Boolean>();

	/**
	 * like % var
	 **/
	public Map<String, String> filterLikeMap = new HashMap<String, String>();

	public RequestParameter(DbType dbType, String url) {

		this.args.put("pageSize", pageSize);
		this.args.put("pageNumber", pageNumber);

		if (null == url || "".equals(url)) {
			url = "";
		}

		this.dbType = dbType;

		switch (dbType) {
		case remotePageDb:
			if (!("" + url).contains("http:")) {
				this.url = UrlPrefix.getUrlPrefix() + "/" + url;
			} else {
				this.url = url;
			}
			break;
		case localPageDb:
			if (!("" + url).contains("content:")) {
				this.url = "content://com.vrv.sqlite/" + url;
			} else {
				this.url = url;
			}
			break;
		case localChartDb:
			if (!("" + url).contains("content:")) {
				this.url = "content://com.vrv.sqlite/" + url;
			} else {
				this.url = url;
			}
			break;
		}
	}

	public RequestParameter(DbType dbType, String url, Map<String, Object> args) {
		this(dbType, url);
		this.args.put("pageSize", pageSize);
		this.args.put("pageNumber", pageNumber);
		getArgs().putAll(args);
	}

	/* function methodes */

	/**
	 * 添加排序字段设置排序方式
	 * 
	 * @param field
	 * @param isAsc
	 */
	public void addSortArgs(String field, boolean isAsc) {
		this.sortMap.put(field, isAsc);
	}

	public void addArgs(String key, Object value) {
		this.args.put(key, value);
	}

	public void addCompareArgs(String field, boolean gt) {
		this.compareMap.put(field, gt);
	}

	public void addFilterLikeArgs(String field, String val) {
		this.filterLikeMap.put(field, val);
	}

	/* Gettings && Settings */

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getPageSize() {
		String val = String.valueOf(args.get("pageSize"));
		if (StringUtils.isNullOrEmpty(val)) {
			return Integer.parseInt(val);
		} else {
			return this.pageSize;
		}

	}

	public void setPageSize(int pageSize) {
		args.put("pageSize", pageSize);
		this.pageSize = pageSize;
	}

	public int getPageNumber() {
		String val = String.valueOf(args.get("pageNumber"));
		if (StringUtils.isNullOrEmpty(val)) {
			return Integer.parseInt(val);
		} else {
			return this.pageNumber;
		}
	}

	public void setPageNumber(int pageNumber) {
		args.put("pageNumber", pageNumber);
		this.pageNumber = pageNumber;
	}

	public Map<String, Object> getArgs() {
		return args;
	}

	public void setArgs(Map<String, Object> args) {
		this.args.putAll(args);
	}

	public DbType getDbType() {
		return dbType;
	}

	public void setDbType(DbType dbType) {
		this.dbType = dbType;
	}

	public Map<String, Boolean> getSortMap() {
		return sortMap;
	}

	public void setSortMap(Map<String, Boolean> sortMap) {
		this.sortMap = sortMap;
	}

	public Map<String, Boolean> getCompareMap() {
		return compareMap;
	}

	public void setCompareMap(Map<String, Boolean> compareMap) {
		this.compareMap = compareMap;
	}

	public Map<String, String> getFilterLikeMap() {
		return filterLikeMap;
	}

	public void setFilterLikeMap(Map<String, String> filterLikeMap) {
		this.filterLikeMap = filterLikeMap;
	}

}